package nexus.com.grid
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import nexus.base.NXSprite;
	import nexus.definition.Align;
	
	
	
	/**
	 * <br>설명 : </br> 
	 * @author winnie
	 */	
	public class GridHeader extends NXSprite
	{
		//-----------------------------------------------------------------------------------
		//
		// private vars
		//
		//-----------------------------------------------------------------------------------
		
		private var _list : Array = [];
		private var _columns : Array = [];
		private var _columnsWids : Array = [];
		//-----------------------------------------------------------------------------------
		//
		// protected vars
		//
		//-----------------------------------------------------------------------------------
		
		
		protected var backGroundClip : DisplayObject;
		private var _tf : TextFormat;
		private var _tfList : Array;
		
		//-----------------------------------------------------------------------------------
		//
		// public vars
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		public function GridHeader()
		{
			super();
			
			this.viewWidth = 500;
			this.viewHeight = 33;
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// setter / getter
		//
		//-----------------------------------------------------------------------------------

		public function get columnsWids():Array
		{
			return _columnsWids;
		}

		public function set columnsWids(value:Array):void
		{
			_columnsWids = value;
		}

		public function set bgClip(value:DisplayObject):void
		{
			if( backGroundClip && this.contains( backGroundClip ) )
			{
				removeChild( backGroundClip );
				backGroundClip = null;
			}
			
			backGroundClip = value;
			addChildAt( backGroundClip, 0 );
			backGroundClip.width = viewWidth;
		}
		
		public function get bgClip():DisplayObject
		{
			if( backGroundClip == null )
			{
				backGroundClip = new GRID_TITLE_BG() as DisplayObject;
				addChildAt( backGroundClip, 0 );
			}
			return backGroundClip;
		}

		public function get list():Array
		{
			return _list;
		}

		public function set list(value:Array):void
		{
			_list = value;
			updateDisplayList();
		}

		public function get columns():Array
		{
			return _columns;
		}
		
		public function set columns(value:Array):void
		{
			_columns = value;
			
			setGridTitle();
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// public function
		//
		//-----------------------------------------------------------------------------------
		
		

		override public function updateDisplayList():void
		{
			super.updateDisplayList();
			
			// 타이틀 셋팅을 먼저 하고,
			setGridTitle();
			
			// 정렬하기.
			var sum : Number = 0;
			var len:int = _list.length;
			for (var i:int = 0; i < len; i++) 
			{
				// 가로 정렬.
				var hAlign : String = ( _columns[ i ] as GridColumn ).alignTitleHorizontal;
				if( hAlign == Align.LEFT ) _list[ i ].x = sum;
				else if( hAlign == Align.RIGHT ) _list[ i ].x = sum+_columnsWids[ i ] - _list[i].width;
				else _list[ i ].x = ( _columnsWids[ i ]-_list[i].width )/2+sum;
				
				// 세로정렬
				var vAlign : String = ( _columns[ i ] as GridColumn ).alignTitleVertical;
				if( vAlign == Align.TOP ) _list[ i ].y = 0;
				if( vAlign == Align.MIDDLE ) _list[ i ].y = ( viewHeight-_list[ i ].height )/2;
				if( vAlign == Align.BOTTOM ) _list[ i ].y = viewHeight-_list[ i ].height;
				
				sum += _columnsWids[ i ];
			}
			
			
			bgClip.width = this.viewWidth;
		}
		
		
		public function set defaultTextFormat( tf : TextFormat ):void
		{
			_tf = tf;
		}
		
		public function get defaultTextFormat():TextFormat
		{
			return ( _tf ) ? _tf : new TextFormat("돋움", 12, 0x444444, true );
		}
		
		public function set textFormatList( list : Array ):void
		{
			_tfList = list;
		}
		
		public function get textFormatList():Array
		{
			return _tfList;
		}
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// protected function
		//
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// private function
		//
		//-----------------------------------------------------------------------------------
		
		
		// 그리드 타이틀 셋팅
		private function setGridTitle():void
		{
			removeAndDestoryChild();
			
			
			var len:int = _columns.length;
			var tf : TextFormat;
			for (var i:int = 0; i < len; i++) 
			{
				var colum : GridColumn = _columns[ i ] as GridColumn;
				
				// 만약 타이틀이 문자열이면,
				if( colum.title is String )
				{
					var txt : TextField = new TextField();
					txt.autoSize = TextFieldAutoSize.LEFT;
					txt.mouseEnabled = false;
					txt.mouseWheelEnabled = false;
					
					if( _tfList && _tfList.length - 1 >= i && _tfList[ i ] )
					{
						tf = _tfList[ i ] as TextFormat;
					}
					else
					{
						tf = defaultTextFormat;
					}
					txt.defaultTextFormat = tf;
					txt.setTextFormat( tf );
					
					txt.text = colum.title as String;
					addChild( txt );
					_list.push( txt );
				}
				else if( colum.title is DisplayObject )
				{
					addChild( colum.title as DisplayObject );
					_list.push( colum.title );
				}
			}
		}
		
		
		// 현재있는 타이틀 제거
		private function removeAndDestoryChild():void
		{
			var len:int = _list.length;
			for (var i:int = 0; i < len; i++) 
			{
				var obj : DisplayObject = _list.shift() as DisplayObject;
				
				if( this.contains( obj ) )
				{
					removeChild( obj );
					obj = null;
				}
			}
		}
		
		//-----------------------------------------------------------------------------------
		//
		// event handler
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// destory
		//
		//-----------------------------------------------------------------------------------
		
		override public function destory():void
		{
			super.destory();
		}
	}
}